﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Collections;
using System.Xml.Linq;
using System.IO;
using StripeOne.Core.Cache;
using StripeOne.Core.Config;

namespace StripeOne.Web.Base.Configuration
{
    public class StripeOneXml
    {

        public StripeOneXml(string section)
        {
            this.Section = section;
        }

        public StripeOneXml(string file, bool isDictionary = false)
        {
            this.File = file;
            this.IsDictionary = isDictionary;
        }

        public StripeOneXml(string file, string section, bool isDictionary = false)
        {
            this.File = file;
            this.Section = section;
            this.IsDictionary = isDictionary;
        }

        private string File { get; set; }
        private string Section { get; set; }
        private bool IsDictionary { get; set; }

        private XDocument LoadXml()
        {
            return ConfigXml.LoadXml(this.File, this.IsDictionary, 500);
        }

        private object GetValue(string key)
        {
            XDocument xml = LoadXml();
            XElement xmlElements = null;

            if (xml != null)
            {
                xmlElements = (from item in xml.Root.Descendants(this.Section)
                               select item.Element(key)).FirstOrDefault();

                if (xmlElements == null)
                {
                    xmlElements = (from item in xml.Root.Descendants("default")
                                   select item.Element(key)).FirstOrDefault();

                }
            }
            return xmlElements != null ? xmlElements.Value : "[" + this.Section + "|default" + "/" + key + "]";

        }

        protected object GetAttribute(string name)
        {
            XDocument xml = LoadXml();
            XAttribute attributeValue = null;

            if (xml != null)
            {
                attributeValue = (from item in xml.Root.Descendants(this.Section)
                                  select item.Attribute(name)).FirstOrDefault();

                if (attributeValue == null)
                {
                    attributeValue = (from item in xml.Root.Descendants("default")
                                      select item.Attribute(name)).FirstOrDefault();
                }
            }

            return attributeValue != null ? attributeValue.Value : "[" + this.Section + "|default" + "/" + name + "]";
        }

        protected string GetValue(string section, string key)
        {
            XDocument xml = LoadXml();

            var xmlElements = (from item in xml.Root.Descendants(section)
                               select item.Element(key)).FirstOrDefault();

            return xmlElements != null ? xmlElements.Value : "[" + section + "/" + key + "]";
        }

        protected string GetString(string key)
        {
            return (string)this.GetValue(key);
        }

        protected int GetInt(string key)
        {
            int result = 0;
            int.TryParse(this.GetString(key), out result);
            return result;
        }

        protected decimal GetDecimal(string key)
        {
            decimal result = 0;
            decimal.TryParse(this.GetString(key), out result);
            return result;
        }

        protected double GetDouble(string key)
        {
            double result = 0;
            double.TryParse(this.GetString(key), out result);
            return result;
        }

        protected float GetFloat(string key)
        {
            float result = 0;
            float.TryParse(this.GetString(key), out result);
            return result;
        }

        protected bool GetBool(string key)
        {
            bool result;
            bool.TryParse(this.GetString(key), out result);
            return result;
        }

        protected DateTime GetDateTime(string key)
        {
            DateTime result = new DateTime();
            DateTime.TryParse(this.GetString(key), out result);
            return result;
        }

        protected object GetObject(string key)
        {
            return this.GetValue(key);
        }
    }
}